package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode98 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static boolean BST(TreeNode root, TreeNode min, TreeNode max) {
        if (root==null){
            return true;
        }
        if (min!=null&&min.val>=root.val){
            return false;
        }
        if (max!=null&&max.val<=root.val){
            return false;
        }
        return BST(root.left,min,root)&&BST(root.right,root,max);
    }


    public static boolean isValidBST(TreeNode root) {
        return BST(root,null,null);
    }
}